package com.atguigu.gmall.pms.service.impl;

import com.atguigu.gmall.pms.entity.AttrEntity;
import com.atguigu.gmall.pms.entity.SkuAttrValueEntity;
import com.atguigu.gmall.pms.entity.SpuAttrValueEntity;
import com.atguigu.gmall.pms.mapper.AttrMapper;
import com.atguigu.gmall.pms.mapper.SkuAttrValueMapper;
import com.atguigu.gmall.pms.mapper.SpuAttrValueMapper;
import com.atguigu.gmall.pms.vo.AttrGroupVO;
import com.atguigu.gmall.pms.vo.AttrValueVo;
import com.atguigu.gmall.pms.vo.ItemGroupVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.gmall.common.bean.PageResultVo;
import com.atguigu.gmall.common.bean.PageParamVo;

import com.atguigu.gmall.pms.mapper.AttrGroupMapper;
import com.atguigu.gmall.pms.entity.AttrGroupEntity;
import com.atguigu.gmall.pms.service.AttrGroupService;


@Service("attrGroupService")
public class AttrGroupServiceImpl extends ServiceImpl<AttrGroupMapper, AttrGroupEntity> implements AttrGroupService {

    @Autowired
    private AttrMapper attrMapper;

    @Autowired
    private SpuAttrValueMapper spuAttrValueMapper;

    @Autowired
    private SkuAttrValueMapper skuAttrValueMapper;

    @Override
    public PageResultVo queryPage(PageParamVo paramVo) {
        IPage<AttrGroupEntity> page = this.page(
                paramVo.getPage(),
                new QueryWrapper<AttrGroupEntity>()
        );

        return new PageResultVo(page);
    }

    @Override
    public List<AttrGroupVO> queryAttrGroupsByCid(Long catId) {

        //查询分组信息
        List<AttrGroupEntity> groupEntities = this.list(new LambdaQueryWrapper<AttrGroupEntity>()
                                                   .eq(AttrGroupEntity::getCategoryId,catId));

        //将entity转为vo对象
        List<AttrGroupVO> VO = groupEntities.stream().map(attrGroupEntity -> {
            //查询规格参数信息 只有type为1才展示
            List<AttrEntity> attrEntities = attrMapper.selectList(new LambdaQueryWrapper<AttrEntity>().eq(AttrEntity::getGroupId, attrGroupEntity.getId())
                    .eq(AttrEntity::getType, "1"));

            AttrGroupVO attrGroupVO = new AttrGroupVO();
            BeanUtils.copyProperties(groupEntities, attrGroupVO);
            attrGroupVO.setAttrEntities(attrEntities);
            return attrGroupVO;
        }).collect(Collectors.toList());
        return VO;
    }

    @Override
    public List<ItemGroupVo> queryGroupWithAttrValueByCidAndSkuIdAndSpuId(Long cid, Long skuId, Long spuId) {
        //查询分类下的所有分组
        List<AttrGroupEntity> attrGroupEntities = this.list(new LambdaQueryWrapper<AttrGroupEntity>().eq(AttrGroupEntity::getCategoryId, cid));
        if (CollectionUtils.isEmpty(attrGroupEntities)){
            return null;
        }
        //将分组的集合 转化为vo集合
        return attrGroupEntities.stream().map(attrGroupEntity -> {
            ItemGroupVo itemGroupVo = new ItemGroupVo();
            itemGroupVo.setId(attrGroupEntity.getId());
            itemGroupVo.setName(attrGroupEntity.getName());

            //遍历分组集合 查询组下的所有规格参数信息
            List<AttrEntity> attrEntities = attrMapper.selectList(new LambdaQueryWrapper<AttrEntity>().eq(AttrEntity::getGroupId, attrGroupEntity.getId()));
            if (CollectionUtils.isEmpty(attrEntities)){
                return itemGroupVo;
            }

            //获取规格参数的id列表
            List<Long> attrIds = attrEntities.stream().map(AttrEntity::getId).collect(Collectors.toList());
            //根据规格参数id列表以及spuid查询所有基本属性的规格参数的值
            List<AttrValueVo> attrValueVos = new ArrayList<>();
            List<SpuAttrValueEntity> spuAttrValueEntities = spuAttrValueMapper.selectList(new LambdaQueryWrapper<SpuAttrValueEntity>()
                    .in(SpuAttrValueEntity::getAttrId, attrIds).eq(SpuAttrValueEntity::getSpuId, spuId));
            //将实体类集合转换为vo集合
            if (!CollectionUtils.isEmpty(spuAttrValueEntities)){
                attrValueVos.addAll(spuAttrValueEntities.stream().map(spuAttrValueEntity -> {
                    AttrValueVo attrValueVo = new AttrValueVo();
                    BeanUtils.copyProperties(spuAttrValueEntity,attrValueVo);
                    return attrValueVo;
                }).collect(Collectors.toList()));
            }

            //根据规格参数id列表以及skuid查询所有销售属性的规格参数的值
            List<SkuAttrValueEntity> skuAttrValueEntities = skuAttrValueMapper.selectList(new LambdaQueryWrapper<SkuAttrValueEntity>()
                    .in(SkuAttrValueEntity::getAttrId, attrIds).eq(SkuAttrValueEntity::getSkuId, skuId));
            if (!CollectionUtils.isEmpty(skuAttrValueEntities)){
                attrValueVos.addAll(skuAttrValueEntities.stream().map(skuAttrValueEntity -> {
                    AttrValueVo attrValueVo = new AttrValueVo();
                    BeanUtils.copyProperties(skuAttrValueEntity,attrValueVo);
                    return attrValueVo;
                }).collect(Collectors.toList()));
            }
            itemGroupVo.setAttrValues(attrValueVos);
            return itemGroupVo;
        }).collect(Collectors.toList());
    }
}